package com.example.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Userlogin;
import com.example.mapper.UserMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
//此部分为用户登录所用到的controller类，主要是登录和注册两个功能。

    @Resource
    UserMapper userMapper ;

    @PostMapping("/login")
    public Result login(@RequestBody Userlogin userlogin) {
      Userlogin res = userMapper.selectOne(Wrappers.<Userlogin>lambdaQuery().eq(Userlogin::getUser_id, userlogin.getUser_id()).eq(Userlogin::getUser_password, userlogin.getUser_password()));
        if (res == null) {
            return Result.error("用户名或密码错误");
        }
        return Result.success();
    }

    @PostMapping("/register")
    public Result register(@RequestBody Userlogin userlogin) {
        Userlogin res = userMapper.selectOne(Wrappers.<Userlogin>lambdaQuery().eq(Userlogin::getUser_id, userlogin.getUser_id()));
        if (res != null) {
            return Result.error("用户重复");
        }
        if(userlogin.getUser_password()==null)
        {
            return Result.error("未输入密码");
        }
        userMapper.insert(userlogin);
        return Result.success();
    }

























 /*   @Autowired
    private UserServiceImpl userService;

    @GetMapping("/page")
    public Page<Userlogin> page(

            @RequestParam(defaultValue = "") String user_id,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "15") Integer size

    )
    {
        return userService.page(current,size,user_id);

    }

    @PostMapping ("/save")
    public boolean save (@RequestBody Userlogin userlogin){
        return userService.saveOrUpdateById(userlogin);
    }

    @PostMapping ("/delete")
    public boolean delete (@RequestBody List<Integer> user_ids){
        return userService.deleteBatchIds(user_ids);
    }



    @Resource
    UserMapper userMapper;


   @PostMapping ("/login")
    public Userlogin login(@RequestBody Userlogin userlogin){

       Userlogin dbUser = userMapper.getUserByIdAndPassword(userlogin.getUser_id(),userlogin.getUser_password());
               return dbUser ;
   }*/



}